<template>
  <div class="create-order">
    <nav-bar>
      <template v-slot:left>&lt;</template>
      <template v-slot:default>订单预览</template>
    </nav-bar>
    <div class="address-wrap">
      <div class="name" @click="goTo">
        <span>{{state.address.name}}</span>
        <span>{{state.address.phone}}</span>
      </div>
      <div class="address">
        {{state.address.province}} {{state.address.city}} {{state.address.county}} {{state.address.address}}
      </div>
      <van-icon class="arrow" name="arrow" />
    </div>
    <div class="good">
      <div class="good-item" v-for="(item, index) in state.cartList" :key="index">
        <div class="good-img">
          <img :src="item.goods.cover_url" alt="" />
        </div>
        <div class="good-desc">
          <div class="good-title">
            <span>{{item.goods.title}}</span>
            <span>x{{item.num}}</span>
          </div>
          <div class="good-btn">
            <div class="price">
              ￥{{item.goods.price}}.00
            </div>
          </div>
        </div>
      </div>
    </div>
    <van-submit-bar
      class="submit-all"
      :price="total * 100"
      button-text="生成订单"
      @submit="handleCreateOrder"
    >
      商品金额
    </van-submit-bar>
    <van-popup
      closeable
      :close-on-click-overlay="false"
      :show="false"
      position="bottom"
      :style="{ height: '30%' }"
      @close="close"
    >
      <div :style="{ width: '90%', margin: '0 auto', padding: '50px 0' }">
        <van-button :style="{ marginBottom: '10px' }" color="#1989fa" block>
          支付宝支付
        </van-button>
        <van-button color="#4fc08d">
          微信支付
        </van-button>
      </div>
    </van-popup>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import { getOrderPreview, createOrder} from "network/order";
import { Toast } from "vant";
import { useRouter, useRoute } from "vue-router";
import { computed, onMounted, reactive } from "vue";
import {useStore} from 'vuex';
export default {
  name: "",
  components: {
    NavBar,
  },
  setup() {
    const router = useRouter();
    const route = useRoute();
    const store = useStore();
    const state = reactive({
      cartList: [],
      address: {},
    });
    const init = () => {
      Toast.loading({
        message: "加载中。。。",
        forbidClick: true,
      })

      getOrderPreview().then((res) => {
        console.log(res);
        let address = res.address.filter(n=> n.is_default == '1');
        if(address.lenght == 0) {
          state.address = {
            address: '还没有设置默认地址，请选择或填写地址信息',
          }
        }else {
          state.address = address[0];
        }

        state.cartList = res.carts;

        Toast.clear();
      })
    }

    onMounted(() => {
      init()
    });
    const goTo = () => {
      console.log('11')
      router.push({path: 'addresslist'})
    };
    const handleCreateOrder = () => {
      const params = {
        address_id: state.address.id,
      }
      createOrder(params).then(res => {
        Toast('创建订单成功~');
        store.dispatch('updataCart');
      })

    };
    const close = () => {};

    const total= computed(()=> {
      let sum = 0;
      state.cartList.forEach((item) => {
        sum += parseInt(item.num) * parseFloat(item.goods.price); 
      });

      return sum
    })
    return { state, goTo, handleCreateOrder, close, total };
  },
};
</script>

<style lang="scss" scoped>
.create-order {
  background: #f9f9f9;
  .address-wrap {
    text-align: left;
    margin-bottom: 20px;
    background: #fff;
    position: relative;
    margin-top: 45px;
    font-size: 14px;
    padding: 15px;
    color: #222333;
    .name,
    .address {
      margin: 10px 0;
    }
    .arrow {
      position: absolute;
      right: 10px;
      top: 50%;
      transform: translate(-50%);
      font-size: 20px;
    }
  }
  .good {
    margin-bottom: 10px;

    .good-item {
      padding: 10px;
      background-color: #fff;
      display: flex;
      .good-img {
        img {
          width: 100px;
          height: auto;
        }
      }
      .good-desc {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;
        padding: 20px;
        .good-title {
          display: flex;
          justify-content: space-between;
        }
        .good-btn {
          display: flex;
          justify-content: space-between;
          .price {
            font-size: 16px;
            color: red;
            line-height: 28px;
          }
          .van-icon-delete {
            font-size: 20px;
            margin-top: 4px;
          }
        }
      }
    }
  }
  .van-submit-bar {
    bottom: 50px;
  }
}
</style>
